package com.ophiux.cs.infrastructure.repositories.consult;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ophiux.cs.domain.consult.model.aggregate.CSUserInfo;
import com.ophiux.cs.domain.consult.model.entity.CSUser;
import com.ophiux.cs.domain.consult.model.entity.CSUserLoginRecords;
import com.ophiux.cs.domain.consult.repositories.IUserInfoRepository;
import com.ophiux.cs.infrastructure.mapper.consult.CSUserLoginRecordsMapper;
import com.ophiux.cs.infrastructure.mapper.consult.CSUserMapper;
import com.ophiux.utils.exception.OphiuxDaoException;
import com.ophiux.utils.exception.OphiuxRepositoryException;


/**
 * 用户信息仓储
 * @author chenh
 *
 */
@Repository("cSUserInfoRepository")
public class CSUserInfoRepository implements IUserInfoRepository{

	@Autowired
	CSUserLoginRecordsMapper cSUserLoginRecordsMapper;	//用户登录记录
	
	@Autowired
	CSUserMapper cSUserMapper;	//用户信息

	@Override
	public void save(CSUser csUser) throws OphiuxRepositoryException {
		
		try {
			//判断用户是否存在，存在，更新；不存在，新增
			CSUser _csUser = cSUserMapper.getCSUserByTargetID(csUser.getTARGET_ID());
			if( _csUser == null ) {
				//不存在，新增
				//新增用户
				cSUserMapper.insert(csUser);
			}else {
				//存在，更新用户
				cSUserMapper.update(csUser);
			}
			
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			throw new OphiuxRepositoryException("咨询用户存储异常", e);
		}
		
	}

	@Override
	public CSUser getCSUserByTargetID(String targetID) throws OphiuxRepositoryException {
		
		CSUser _csUser = null;
		try {
			 _csUser = cSUserMapper.getCSUserByTargetID(targetID);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			throw new OphiuxRepositoryException("咨询用户获取异常",e);
		}
		
		return _csUser;
	}

	@Override
	public CSUser getCSUserByTargetACC(String targetACC) throws OphiuxRepositoryException {
		
		CSUser _csUser = null;
		try {
			 _csUser = cSUserMapper.getCSUserByTargetACC(targetACC);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			throw new OphiuxRepositoryException("咨询用户获取异常",e);
		}
		
		return _csUser;
		
	}

	@Override
	public CSUserInfo getCSUserInfoByTargetID(String targetACC) throws OphiuxRepositoryException {
		
		CSUserInfo userInfo = new CSUserInfo();
		//根据账号，获取用户基本信息
		CSUser _csUser = getCSUserByTargetACC(targetACC);
		userInfo.setCsUser(_csUser);
		
		//获取最近一次登录信息及当前登录信息
		List<CSUserLoginRecords> csUserLoginRecords = null;
		try {
			csUserLoginRecords
							= cSUserLoginRecordsMapper.getCSUserLoginRecordsByTargetID(_csUser.getTARGET_ID());
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			throw new OphiuxRepositoryException("咨询登录记录获取异常",e);
		}
		
		if(csUserLoginRecords == null || csUserLoginRecords.size() == 0) return userInfo;
		
		//获取当前登录记录，即最新一条且有效
		CSUserLoginRecords curLoginRecord = null;
		CSUserLoginRecords lastLoginRecord = null;
		//理论上，本次
		curLoginRecord = csUserLoginRecords.get(0);
		//理论上，上一次
		if(csUserLoginRecords.size() > 1)
			lastLoginRecord = csUserLoginRecords.get(1);
		
		if(!curLoginRecord.isOnline()) {
			//当前不在线，作为上一次登录
			lastLoginRecord = curLoginRecord;
			//本次登录为空
			curLoginRecord = null;
		}
		userInfo.setCurLoginRecord(curLoginRecord);
		userInfo.setLastLoginRecord(lastLoginRecord);
		
		return userInfo;
	}

	@Override
	public void save(CSUserInfo loginUser) throws OphiuxRepositoryException {
		
		//用户信息保存
		save(loginUser.getCsUser());
		
		try {
			
			//保存登录信息
			//当前登录信息
			CSUserLoginRecords curLoginRecord = loginUser.getCurLoginRecord();
			
			if(curLoginRecord != null) {
				//数据库查询
				CSUserLoginRecords _curLoginRecord = cSUserLoginRecordsMapper
						.getCSUserLoginRecordsByLoginToken(curLoginRecord.getLOGIN_TOKEN());
				
				if(_curLoginRecord == null) {
					//不存在，新增
					cSUserLoginRecordsMapper.insert(curLoginRecord);
				}else {
					//存在，更新
					cSUserLoginRecordsMapper.update(curLoginRecord);
				}
			}
			
			//上一次登录信息
			CSUserLoginRecords lastLoginRecord = loginUser.getLastLoginRecord();
			
			if(lastLoginRecord != null) {
				//数据库查询
				CSUserLoginRecords _lastLoginRecord = cSUserLoginRecordsMapper
						.getCSUserLoginRecordsByLoginToken(lastLoginRecord.getLOGIN_TOKEN());
				
				if(_lastLoginRecord == null) {
					//不存在，新增
					cSUserLoginRecordsMapper.insert(lastLoginRecord);
				}else {
					//存在，更新
					cSUserLoginRecordsMapper.update(lastLoginRecord);
				}
			}
			
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			throw new OphiuxRepositoryException("用户信息保存异常",e);
		}
		
	}
	
}
